package org.jspresso.framework.application.frontend.action.entity.clone;

import java.util.Map;

import org.jspresso.framework.action.IActionHandler;
import org.jspresso.framework.application.frontend.action.FrontendAction;
import org.jspresso.framework.model.component.IComponent;
import org.jspresso.framework.model.entity.IEntity;
import org.jspresso.framework.model.entity.IEntityCloneFactory;
import org.jspresso.model.TestEntity;

public class ChooseEntityToCloneOkAction<E, F, G> extends FrontendAction<E, F, G> {

	private IEntityCloneFactory cloneFactory;

	@Override
	public boolean execute(IActionHandler actionHandler, Map<String, Object> context) {
		TestEntity selectedEntity = getSelectedModel(new int[]{0}, context);
		IComponent clonedComponent = null;
		if(selectedEntity.getValue() instanceof IEntity) {
			clonedComponent = cloneFactory.cloneEntity((IEntity)selectedEntity.getValue(), getBackendController(context).getEntityFactory());
		}
		else {
			clonedComponent = cloneFactory.cloneComponent(selectedEntity.getValue(), getBackendController(context).getEntityFactory());
		}
		setActionParameter(clonedComponent, context);
		return super.execute(actionHandler, context);
	}

	public void setCloneFactory(IEntityCloneFactory cloneFactory) {
		this.cloneFactory = cloneFactory;
	}
}
